package com.blogs.service;



import java.util.Map;

import com.blogs.entity.AppConfig;
import com.blogs.entity.Author;
import com.blogs.entity.MsgEntity;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GetUserInfo {
	public interface IMsgCallBack {
		public void onSuccess(Author author);
		public void onFailed();
	}
	private String url="";
	private GetMsgTask timeLine;
    private IMsgCallBack callback;
    public GetUserInfo(String blogapp,IMsgCallBack callback) {
		// TODO Auto-generated constructor stub
    	this.callback=callback;
		timeLine=new GetMsgTask(taskCallBack);
    	url=AppConfig.GET_USER_INFO;
    	url=url.replace("{0}", blogapp);
	}
    public void loadData()
	{
		timeLine.execute(url);
	}
    private GetMsgTask.onTaskCallBack taskCallBack=new GetMsgTask.onTaskCallBack() {
    	public void onSuccess(String result) {
    		// TODO Auto-generated method stub
    		GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			MsgEntity<Map<String, Object>> msg=null;
			try
			{
				msg=gson.fromJson(result,MsgEntity.class);
			}
			catch(Exception e)
			{
				callback.onFailed();
				return;
			}
			if(msg.op.equals(""))
			{
				callback.onFailed();
				return;
			}
			Author author=new Author(msg.data);
			
			callback.onSuccess(author);
    	}
    	public void onFailed() {
    		// TODO Auto-generated method stub
    		callback.onFailed();
    	}
    };
}
